package com.zhilei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhilei.base.BaseInfoProperties;
import com.zhilei.exceptions.GraceException;
import com.zhilei.grace.result.ResponseStatusEnum;
import com.zhilei.mapper.ResumeEducationMapper;
import com.zhilei.mapper.ResumeMapper;
import com.zhilei.mapper.ResumeProjectExpMapper;
import com.zhilei.mapper.ResumeWorkExpMapper;
import com.zhilei.pojo.Resume;
import com.zhilei.pojo.ResumeEducation;
import com.zhilei.pojo.ResumeProjectExp;
import com.zhilei.pojo.ResumeWorkExp;
import com.zhilei.pojo.bo.EditResumeBO;
import com.zhilei.pojo.vo.ResumeVO;
import com.zhilei.service.ResumeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 简历表 服务实现类
 * </p>
 *
 * @author 志磊
 * @since 2024-09-20
 */
@Slf4j
@Service
public class ResumeServiceImpl extends BaseInfoProperties implements ResumeService {

    @Autowired
    private ResumeMapper resumeMapper;

    @Autowired
    private ResumeWorkExpMapper resumeWorkExpMapper;

    @Autowired
    private ResumeProjectExpMapper resumeProjectExpMapper;

    @Autowired
    private ResumeEducationMapper resumeEducationMapper;

    /**
     * 根据用户ID初始化简历
     *
     * @param userId
     */
    @Transactional
    @Override
    public void initResume(String userId) {
        Resume resume = new Resume();
        resume.setUserId(userId);
        resume.setAdvantage("长得帅！");
        resume.setAdvantageHtml("");
        resume.setSkills("长得帅！");
        resume.setCredentials("骨灰级Java高级开发工程师！");
        resume.setStatus("在职");
        resume.setCreateTime(LocalDateTime.now());
        resume.setRefreshTime(LocalDateTime.now());
        resume.setUpdatedTime(LocalDateTime.now());
        resumeMapper.insert(resume);
    }

    /**
     * 修改个人简历信息
     *
     * @param editResumeBO 简历实体BO类
     */
    @Transactional
    @Override
    public void modify(EditResumeBO editResumeBO) {
        Resume resume = new Resume();
        BeanUtils.copyProperties(editResumeBO, resume);
        resume.setUpdatedTime(LocalDateTime.now());

        resumeMapper.update(resume, new QueryWrapper<Resume>()
                .eq("id", resume.getId())
                .eq("user_id", resume.getUserId()));

        redis.del(REDIS_RESUME_EXPECT + ":" + editResumeBO.getUserId());
    }

    /**
     * 根据用户id查询用户信息
     *
     * @param userId 那个用户的简历
     * @return
     */
    @Override
    public ResumeVO getResume(String userId) {
        ResumeVO resumeVO = new ResumeVO();

        // 1. 查询简历信息
        Resume resume = resumeMapper.selectOne(new QueryWrapper<Resume>()
                .eq("user_id", userId));
        if (resume == null) return null;
        BeanUtils.copyProperties(resume, resumeVO);

        // 2. 查询工作经验
        List<ResumeWorkExp> workExpList = resumeWorkExpMapper.selectList(new QueryWrapper<ResumeWorkExp>()
                .eq("resume_id", resume.getId())
                .eq("user_id", userId));
        resumeVO.setWorkExpList(workExpList);

        // 3. 查询项目经验
        List<ResumeProjectExp> resumeProjectExpLis = resumeProjectExpMapper.selectList(new QueryWrapper<ResumeProjectExp>()
                .eq("user_id", userId)
                .eq("resume_id", resume.getId()));
        resumeVO.setProjectExpList(resumeProjectExpLis);

        // 4. 查询教育经验
        List<ResumeEducation> resumeEducationList = resumeEducationMapper.selectList(new QueryWrapper<ResumeEducation>()
                .eq("user_id", userId)
                .eq("resume_id", resume.getId()));
        resumeVO.setEducationList(resumeEducationList);

        return resumeVO;
    }

    /**
     * 简历刷新
     *
     * @param resumeId
     * @param userId
     */
    @Transactional
    @Override
    public void refresh(String resumeId, String userId) {
        Resume resume = resumeMapper.selectOne(new QueryWrapper<Resume>()
                .eq("user_id", userId));
        if (resume == null) GraceException.disPlay(ResponseStatusEnum.USER_STATUS_ERROR);

        // 创建一个简历的对象
        EditResumeBO editResumeBO = new EditResumeBO();
        editResumeBO.setId(resumeId);
        editResumeBO.setUserId(userId);
        editResumeBO.setRefreshTime(LocalDateTime.now());

        // 2. 调用简历刷新的方法
        this.modify(editResumeBO);
        
    }
}










































